<?php
namespace Home\Controller;
use Think\Controller;

/**
* 测试工具 控制器
*/
class ApiToolController extends HomeBasicController{
	
	public $Project = '';
	public $Module = '';
	public $Function = '';

	public function _initialize(){
		parent::_initialize();
		$this->checkLogin();
		$this->Project = D('Project');
		$this->Module = D('Module');
		$this->Function = D('Function');
		$this->assign('active',2);
	}
	
	/**
	 * 显示测试页面
	 */
	public function index(){
		$mid = session('M_ID');
    	$mtype = session('M_TYPE');
		//查询所有的项目
		if($mtype=='2'){
    		$where['own_m_id'] = $mid;
            $where['is_show'] = 1;
            $where['status'] = 1;
            $where['_logic'] = 'OR';
    		$project_list = $this->Project->selectProject($where,'ctime desc','');
    	}else{
    		$project_list = $this->Project->selectProject('','ctime desc','');
    	}
		$this->assign('project_list',$project_list);
		$this->display();
	}

	/**
	 * ajax获取选中项目下的模块
	 */
	public function ajaxModule(){
		$where['p_id'] = $_POST['pid'];
		$project_info = $this->Project->where($where)->getField('p_api_head');
		$module_list = $this->Module->selectModule($where,'','');
		$this->ajaxReturn(array('molist'=>$module_list,'phead'=>$project_info),'JSON');
	}

	/**
	 * ajax获取选中模块下的方法
	 */
	public function ajaxFun(){
		$where['mo_id'] = $_POST['moid'];
		$module_info = $this->Module->where($where)->getField('mo_e_name');
		$function_list = $this->Function->selectFunction($where,'','');
		$this->ajaxReturn(array('flist'=>$function_list,'moename'=>$module_info),'JSON');
	}

	/**
	*ajax获取方法下的参数
	*/
	public function ajaxParameters(){
		$where['f_id'] = $_REQUEST['f_id'];
		$pas = $this->Function->where($where)->find();
		$parameters = unserialize($pas['f_pa']);
		$ftype = $pas['f_type'];
		$fname = $pas['f_name'];
		$furl = $pas['f_url'];
		$this->ajaxReturn(array('pa'=>$parameters,'ftype'=>$ftype,'fname'=>$fname,'furl'=>$furl),'JSON');
	}

    public function aa(){
        $json_str = '{"flag":"error","message":"\u53c2\u6570\u4e0d\u5b8c\u6574"}';
        dump(js);
    }

	/**
     * 调用curl获取数据
     */
    public function getInfo(){
        $url = $_POST['url'];
        $data = $_POST['paGroup'];
        $method = $_POST['type'];
        $data = send_request($url,$data,'',$method);
        //$arr = array('json'=>$data,'json_ch'=>preg_replace("#\\\u([0-9a-f]{4})#ie", "iconv('UCS-2BE', 'UTF-8', pack('H4', '\\1'))", $data));
		if(!empty($data)){
			if(json_decode($data,true)){
				$arr = array('json'=>$data,'status'=>1);
			}else{
				$arr = array('json'=>$data,'status'=>0);
			}
		}else{
			//服务器超时未响应
			$arr = array('json'=>$data,'status'=>2);
		}


        $this->ajaxReturn($arr); 
    }

}	

?>